from rest_framework import serializers
from apps.coffee.models import fields as fieldsModel
import json

class FieldsSerializer(serializers.ModelSerializer):
    value = serializers.SerializerMethodField()

    class Meta:
        model = fieldsModel
        fields = '__all__'
    
    def get_value(self,obj):
        try:  
            f = getattr(obj,'value','')
            return json.loads(f)
        except (json.JSONDecodeError,TypeError):
            return None